package com.copm.ifm.servers.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.BasePO;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.util.AuthUtil;
import com.copm.ifm.base.service.mp.page.PageProxy;
import com.copm.ifm.servers.sys.dto.SysBrandPageDTO;
import com.copm.ifm.servers.sys.entity.SysBrand;
import com.copm.ifm.servers.sys.mapper.SysBrandMapper;
import com.copm.ifm.servers.sys.service.SysBrandService;
import com.copm.ifm.servers.sys.vo.SysBrandPageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 品牌表 服务实现类
 *
 * @author zzf
 * @since 2020-12-14
 */
@Service
public class SysBrandServiceImpl extends ServiceImpl<SysBrandMapper, SysBrand> implements SysBrandService {

    @Resource
    SysBrandMapper sysBrandMapper;

    /**
     * 分页操作
     * @param pageDTO
     */
    @Override
    public PageVO<SysBrandPageVO> selectForPage(PageDTO<SysBrandPageDTO> pageDTO) {
        return PageProxy.proxy(baseMapper::selectForPage, pageDTO);
    }

    @Override
    public SysBrand insert(SysBrand entity) {
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    public int countById(int id) {
        return sysBrandMapper.selectCount(
                new LambdaQueryWrapper<SysBrand>()
                        .eq(SysBrand::getId, id)
                        .eq(SysBrand::getDeleted, DbConstants.DEFAULT_NO)
        );
    }

    @Override
    public SysBrand selectById(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int updateDeleted(int id) {
        SysBrand sysBrand = new SysBrand();
        sysBrand.setId(id);
        sysBrand.setDeleted(DbConstants.DEFAULT_YES);
        return sysBrandMapper.updateById(sysBrand);
    }

    @Override
    public SysBrandPageVO selectVoById(int id) {
        return sysBrandMapper.selectVoById(id);
    }

    @Override
    public boolean checkCode(String name) {
        SysBrand sysBrand = baseMapper.selectOne(new LambdaQueryWrapper<SysBrand>()
                .eq(SysBrand::getName, name)
                .eq(BasePO::getDeleted, DbConstants.DEFAULT_NO));
        return sysBrand == null;
    }

    @Override
    public void updateEntityById(SysBrand entity) {
        entity.setUpdateBy(AuthUtil.getCurrentUserId());
        entity.setUpdateTime(new Date());
        updateById(entity);
    }
}
